const express = require('express');
const bodyParser = require('body-parser');
const app = express();

// 设置静态文件夹
app.use('/public',express.static('static'));

// 中间件：使用bodyParser中间件转换请求体（application/json）数据
app.use(bodyParser.json());

// 注册统一回复的中间件
const responseMiddleware = require('./middlewares/responseMiddleware');
app.use(responseMiddleware);

// JWT 验证中间件
const { verifyToken } = require('./config/jwtUtil');
app.use(verifyToken);

// 引入路由
const router = require('./routers/userRouter');
app.use('/user', router);

const knowledgeTypeRouter = require('./routers/knowledgeTypeRouter');
app.use('/knowledge', knowledgeTypeRouter);

const questionRouter = require('./routers/questionRouter');
app.use('/question', questionRouter);


// 引入全局错误处理中间件
const globalErrorHandler = require('./middlewares/globalErrorHandler');
app.use(globalErrorHandler);

app.listen(3000, () => {
    console.log('服务启动成功，地址:http://localhost:3000');
});